package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;


import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Recommend;
import cn.com.dashihui.web.service.SpikeRecommendService;
@RequiresAuthentication
public class SpikeRecommendController extends BaseController {
	private SpikeRecommendService sRS = new SpikeRecommendService();
	/*品牌制造商页面*/
	public void index() {
		render("index.jsp");
	}
	/*新鲜好物页面*/
	public void index1() {
		render("fresh/index.jsp");
	}
	/*人气推荐页面*/
	public void index2() {
		render("popular/index.jsp");
	}
	/*专题精选列表*/
	public void index3() {
		render("speciallist/index.jsp");
	}
	/*扶贫专区列表*/
	public void index4() {
		render("poverty/index.jsp");
	}
	/*旅游专区列表*/
	public void index5() {
		render("travel/index.jsp");
	}
	/*拼团专区列表*/
	public void index6() {
		render("groupBuy/index.jsp");
	}
	/*天天特卖专区列表*/
	public void index7() {
		render("cheap/index.jsp");
	}
	/*亿品列表*/
	public void optimization() {
		render("optimization/index.jsp");
	}
	/*优选列表*/
	public void best() {
		render("best/index.jsp");
	}
	/*传媒列表*/
	public void preference() {
		render("preference/index.jsp");
	}
	public void page() {
		String title = getPara("title");
		int group = getParaToInt("group");
		int state=getParaToInt("state");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, sRS.findByPage(pageNum, pageSize, title, group,state));

	}

	public void OutSidePage() {
		String title = getPara("title");
		int group = getParaToInt("group");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, sRS.findOutSideByPage(pageNum, pageSize, title, group));

	}
	public void OutSidePageEge() {
		String title = getPara("title");
		int group = getParaToInt("group");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, sRS.findOutSideByPageEge(pageNum, pageSize, title, group));

	}

	public void doDelete() {
		int id = getParaToInt(0,0);
		if (id != 0 && sRS.Delete(id)) {
			renderSuccess();
			return;

		}
		renderFailed();
	}

	public void toAdd() {
		int group = getParaToInt(0, 0);
		System.out.println("66666"+group);
		if (group != 0) {
			setAttr("group", group);
			switch (group) {
			case 1: {
				render("brand/add.jsp");
				break;
			}
			case 2: {
				render("fresh/add.jsp");
				break;
			}
			case 3: {
				render("popular/add.jsp");
				break;
			}
			case 4: {
				render("speciallist/add.jsp");
				break;
			}
			case 5: {
				render("optimization/add.jsp");
				break;
			}
			case 6: {
				render("preference/add.jsp");
				break;
			}
			case 7: {
				render("best/add.jsp");
				break;
			}
			case 8: {
				render("poverty/add.jsp");
				break;
			}
			case 9: {
				render("travel/add.jsp");
				break;
			}
			case 10: {
				render("groupBuy/add.jsp");
				break;
			}
			case 11: {
				render("cheap/add.jsp");
				break;
			}

			}

		}
	}
	//推荐单个
	public void toEdit() {
		int id = getParaToInt(0,0);
		if(id!=0){
			setAttr("object",sRS.findById(id));
		}
		render("edit.jsp");
	}
	//推荐本页
	public void toEditAll() {
		String adIds = getPara("adIds");
		String[] sadIds = adIds.split(",");
		Recommend listRecommend=new Recommend();
		List<Recommend> list = new ArrayList<Recommend>();
		for (String id : sadIds) {
			System.out.println(id);
			String recommend="1";
			listRecommend.set("id",id).set("recommend", recommend);
			sRS.edit(listRecommend);
			int intId = Integer.parseInt(id);
			Recommend recommend2 = sRS.findById(intId);
			list.add(recommend2);
		}
		renderSuccess(list);
	}
	public void doEdit(){
		String id=getPara("id");
		
		String recommend=getPara("recommend");
		/*String state=getPara("state");*/
/*		String authority=getPara("authority");
*/		/*Pattern pat=Pattern.compile("-?[0-9]+(\\.[0-9]+)?");*/
/*		Matcher isNum=pat.matcher(authority);
*/		if(StrKit.isBlank(id)){
			renderFailed();
			return;
		}/*if(!recommend.equals(0)||!recommend.equals(1)){
			renderResult(1);
			return;
		}*/
		/*if(!isNum.matches()){
			renderResult(1);
			return;
		}*/
		int int1 = Integer.parseInt(id);
			Recommend list=new Recommend().set("id",id)
					.set("recommend", recommend)
					/*.set("state", state)*/
					;
			if(sRS.edit(list)){
				renderSuccess(sRS.findById(int1));
				return;
			}
		
		
	}
	/*添加商品*/
	public void doAddObject() {
		String group = getPara("group");
		String goodsType = getPara("goodsType");
		String[] checkcode = getParaValues("checkbox");
		System.out.println(checkcode+"checkcodecheckcodecheckcodecheckcodecheckcode");
		Pattern pat= Pattern.compile("[0-9]*");
		Matcher isNum=pat.matcher(StringUtils.join(checkcode));
		if(StrKit.isBlank(goodsType)){
			renderFailed();
			return;
		}
		if(StrKit.isBlank(group)){
			renderFailed();
			return;
		}
		/*判断checkcode输入的字符串是否都是数字构成的*/
		if(!isNum.matches()){
			renderResult(1);
			return;
		}
		if (checkcode != null && checkcode.length != 0 && sRS.addObject(checkcode, group,goodsType)) {
			/*sRS.addObject(checkcode,group); */
			renderSuccess();
			return;
		}
		System.out.println("没有添加");
		renderFailed();
	}
	public void doSort() {

		Map<String, String[]> params = getParaMap();
		if (params != null && params.size() != 0 && params.containsKey("sortKey")) {
			String sortKey = params.get("sortKey")[0];
			Map<String, String> sortMap = new HashMap<String, String>();
			for (String key : params.keySet()) {
				if (!key.equals("sortKey")) {
					String id = key.replace(sortKey, "");
					String no = params.get(key)[0];
					if (StrKit.isBlank(no) || no.length() > 3) {
						no = "0";
					}
					sortMap.put(id, no);
				}
			}
			sRS.sortList(sortMap);
			renderSuccess();
			return;
		}
		renderFailed();
	}

}
